<?php
  define('ACC', true);
require('./include/init.php');
session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];
//$userid = 26;

$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id']; 
$company_y = $companyinfo['company_y'];
$company_q = $companyinfo['company_q'];

$cashinfo = $company->getcash_equivalent($companyid);
$ttlcashequivalent = 0;
foreach($cashinfo as $k=>$v){ 
    if(($k == '库存现金') || ($k == '银行存款')){
        $ttlcashequivalent = $ttlcashequivalent + $v;   
    }
}

$rawmateriallist = $company->getelementlist('raw_material');

$rawmaterialinstock = $company->getrawmaterialinstock($companyid);
$rawmaterialontheway = $company->getrawmaterialontheway($companyid);

//echo '<h4>目前可供选择的原材料有以下' . count($rawmateriallist) . '种：</h4>';
//echo '<form name="raw_material" action="purchaseAct.php" method="post">

echo ' <div id = "rawmaterialupdate"><form action = "#"><table class="table table-bordered">
          <tr><td>原材料类别</td><td>原材料价格</td><td>目前入库</td><td>目前在途</td><td>采购数量输入</td> </tr>';
foreach($rawmateriallist  as $k=>$v){
    $rawmaterialname = $v['element_name'];
    $rawmaterialprice =  $v['raw_material_price'];
    /*
    $numofrawmaterial = 0;
    foreach($rawmaterialinfo as $key =>$value){
         if($value['element_id'] == $v['element_id']){
             $numofrawmaterial++;
         } 
    }
    */
    $numofrawmaterialinstock = 0;
    $numofrawmaterialontheway = 0;
    if(array_key_exists($rawmaterialname, $rawmaterialinstock)){
        $numofrawmaterialinstock =  $rawmaterialinstock[$rawmaterialname];
    }
    if(array_key_exists($rawmaterialname, $rawmaterialontheway)){
        $numofrawmaterialontheway =  $rawmaterialontheway[$rawmaterialname];
    }
                                                                                                                                                                              
    echo'<tr><td>' . $rawmaterialname . '</td><td>' . $rawmaterialprice . '</td><td><div id = "numof' .  $v['element_id'] . 'instock">' . $numofrawmaterialinstock . '</div></td><td><div id = "numof' . $v['element_id'] .  'ontheway">' .$numofrawmaterialontheway  . '</div></td><td><input type="text" id="' . $v['element_id']. '" /></td></tr>' ;
}          
      
echo '</table></form></div><button type="button" id="sell-rawmaterial" class="btn btn-primary">买入</button><br/>';


echo '          
<script>
$("#sell-rawmaterial").click(function(){
               $.post("purchaserawmaterial1.php", 
                       {';
foreach($rawmateriallist as $k=>$v){
    echo $v['element_id'] . ':$("#' . $v['element_id'] . '").val(),';
}     
echo '                       
                       },                 
                       function (data, textStatus){
                            $.each(data,function(name,value){ 
                            var anchor = "#" + name;
                            $(anchor).html(value);
                            })
                       },
                       "json"
               );
          });
</script>
';  
?>
